/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mytv.backbean;

import com.mytv.entity.Config;
import com.mytv.entity.MovieUser;
import com.mytv.service.ConfigService;
import java.io.File;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Sirius
 */
@SessionScoped
@ManagedBean
public class SessionBean extends BackBean {
    
    public static final String BACK = "..";
    
    private MovieUser user;
    private String path;

    public SessionBean() {
        Logger.getLogger(SessionBean.class.getName()).log(Level.INFO, "Init session bean");
    }

    /**
     * @return the user
     */
    public MovieUser getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(MovieUser user) {
        this.user = user;
    }
    
    /**
     * @return is logged
     */
    public boolean isLogged() {
        return user != null;
    }
    
    /**
     * Disconnect user
     */
    public void disconnect() {
        user = null;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        if(BACK.equals(path) && this.path != null) {
            int index = this.path.lastIndexOf(File.separator);
            if(index >= 0) {
                path = this.path.substring(0, index);
            } else {
                path = null;
            }
        }
        else if(this.path != null) {
            path = this.path + File.separator + path;
        }
        this.path = path;
    }
    
    public boolean isPathNull() {
        return path == null;
    }
    
    private ConfigService getConfigService() {
        return (ConfigService)getSpringBean(ConfigService.class);
    }
    
    public String getRootPathConfig() {
        ConfigService service = getConfigService();
        if(service != null) {
            Config config = service.getConfig(ConfigService.PATH);
            if(config != null && config.getValue() != null && !config.getValue().isEmpty()) {
                String path = config.getValue().trim();
                if(!path.endsWith(File.separator)) {
                    path += File.separator;
                }
                return path;
            }
        }
        return null;
    }
    
    public File getFile() {
        if(path != null) {
            String configPath = getRootPathConfig();
            if(configPath != null) {
                File pathFile = new File(configPath, path);
                if(pathFile.exists()) {
                    return pathFile;
                }
            }
        }
        return null;
    }
    
    public boolean isFile() {
        File file = getFile();
        if(file != null) {
            return file.isFile();
        }
        return false;
    }
    
    public boolean isDirectory() {
        File file = getFile();
        if(file != null) {
            return file.isDirectory();
        }
        return false;
    }

}